package edu.hawaii.ics414;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;

public class CalendarActivity extends Activity {

  private CalendarView calendar;
  private JSONArray jArray;
  private final String url = "http://islandtabs.dyndns.org/fetch.php";

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calendar);
    StrictMode.enableDefaults();
    calendar = (CalendarView) findViewById(R.id.calendarView1);
    DataRequester dr = new DataRequester();
    populate(dr.getData(url));
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
  }

  private void populate(String events) {
    try {
      jArray = new JSONArray(events);
      calendar.setOnDateChangeListener(new OnDateChangeListener() {
        public void onSelectedDayChange(CalendarView arg0, int year, int month, int dayOfMonth) {
          Intent intent = new Intent("edu.hawaii.ics414.CALENDARLIST");
          CalendarList list = new CalendarList();
          SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
          list.setDate(year + "-" + (month + 1) + "-" + dayOfMonth);
          try {
            for (int i = 0; i < jArray.length(); i++) {
              
              String eventDate = jArray.getJSONObject(i).getString("date");
              String day = format.format(format.parse(year + "-" + (month + 1) + "-" + dayOfMonth));
              if (eventDate.equals(day)) {
                HashMap<String, String> event = new HashMap<String, String>();
                event.put("title", jArray.getJSONObject(i).getString("title"));
                event.put("description", jArray.getJSONObject(i).getString("description"));
                event.put("date", jArray.getJSONObject(i).getString("date"));
                event.put("venue", jArray.getJSONObject(i).getString("venue"));
                list.put(event);
              }
            }
          }
          catch (JSONException e) {
            Log.e("json error", "Error parsing data " + e.toString());
          }
          catch (ParseException e) {
            Log.e("parse error", "Error parsing data " + e.toString());
          }
          intent.putExtra("calendar", list);
          startActivity(intent);
        }
      });
    }
    catch (Exception e) {
      Log.e("log_tag", "Error parsing data " + e.toString());
    }
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    super.onOptionsItemSelected(item);

    switch (item.getItemId()) {
    case R.id.home:
      Intent main = new Intent(getApplicationContext(), MainActivity.class);
      main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      finish();
      startActivity(main);
      break;

    case R.id.calendar:
      Intent calendar = new Intent(getApplicationContext(), CalendarActivity.class);
      calendar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      finish();
      startActivity(calendar);
      break;

    /*case R.id.nearbyevents:
      Intent nearbyEvents = new Intent(getApplicationContext(), NearbyEventsActivity.class);
      nearbyEvents.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      finish();
      startActivity(nearbyEvents);
      break;

    case R.id.info:
      Intent info = new Intent(getApplicationContext(), InfoActivity.class);
      info.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      finish();
      startActivity(info);
      break;*/
    }
    return true;
  }
}
